﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Meebey.SmartIrc4net;
using System.Threading; 


namespace IrcYo
{
    public partial class IrcChannelControl : UserControl
    {
        private IrcClient irc;
        private Channel tmpChannel;
        private string channel = "";
        List<ChannelUser> users = new List<ChannelUser>();

        public List<ChannelUser> Users
        {
            get { return users; }
        }

        public string Channel
        {
            get { return channel; }
        }

        public IrcChannelControl(IrcClient irc, string channel)
        {
            InitializeComponent();
            this.channel = channel; 
            this.irc = irc;

            irc.RfcJoin(channel); 
        }

        public void updateUser()
        {
            tmpChannel = irc.GetChannel(channel);
            listUsers.Items.Clear();
            foreach (ChannelUser user in tmpChannel.Users.Values)
            {
                users.Add(user); 
                listUsers.Items.Add(user.Nick);
            }
        }



        public void addMessage(IrcMessageData msg)
        {
            addMessage(msg.Nick, msg.Message); 
        }
        public void addMessage(string msg)
        {
            addMessage(irc.Nickname, msg); 
        }
        public void addMessage(string nick,string msg)
        {
            DateTime now = DateTime.Today; 
            listChannel.Items.Add("["+now.ToShortTimeString()+"]<" + nick + "> " + msg);
        }


        #region Events
        public void OnJoin(JoinEventArgs e)
        {
            updateUser();
        }
        public void OnChannelMessage(IrcEventArgs e)
        {
                updateUser();
                addMessage(e.Data); 
        }

        #endregion

        private void btnSend_Click(object sender, EventArgs e)
        {
            irc.SendMessage(SendType.Message, channel, txtMessage.Text);
            addMessage(txtMessage.Text);             
        }

        private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
    
    }
}
